package com.mnt.washio.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mnt.washio.R;
import com.mnt.washio.utils.SessionManager;
import com.mnt.washio.utils.Statics;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class MyAccountActivity extends BaseActivity implements OnClickListener {

	private ParseUser mCurUser;
	private EditText mEdtFirstName, mEdtLastName, mEdtEmail, mEdtPhone;
	private Button mBtnUpdateAccount;
	private Button mBtnLogout;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_account);
		initView();
		initListeners();
		initProperties();
	}

	private void initProperties() {
		mCurUser = ParseUser.getCurrentUser();
		if(mCurUser != null){
			mEdtFirstName.setText(mCurUser.getString("first_name"));
			mEdtLastName.setText(mCurUser.getString("last_name"));
			mEdtEmail.setText(mCurUser.getEmail());
			mEdtPhone.setText(mCurUser.getString("phone"));
		}
	}

	@Override
	protected void initView() {
		super.initView();
		((TextView) findViewById(R.id.tv_title_bar)).setText("My Account");

		mEdtFirstName = (EditText) findViewById(R.id.edt_first_name);
		mEdtLastName = (EditText) findViewById(R.id.edt_last_name);
		mEdtEmail = (EditText) findViewById(R.id.edt_email);
		mEdtPhone = (EditText) findViewById(R.id.edt_phone);

		mBtnUpdateAccount = (Button) findViewById(R.id.btn_update_acc);
		mBtnLogout = (Button) findViewById(R.id.btn_log_out);

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Updating...");
	}

	private void initListeners() {
		((ImageView) findViewById(R.id.imv_menu)).setOnClickListener(this);
		mBtnUpdateAccount.setOnClickListener(this);
		mBtnLogout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imv_menu:
			finish();
			break;

		case R.id.btn_update_acc:
			validateAndUpdateAccount();
			break;
		case R.id.btn_log_out:
			SessionManager.destroySession();
			ParseUser.logOut();
			startActivity(new Intent(this, LoginActivity.class));
			finish();
			break;
		}
	}

	private void validateAndUpdateAccount() {
		String email = mEdtEmail.getText().toString().trim();
		String firstName = mEdtFirstName.getText().toString().trim();
		String lastName = mEdtLastName.getText().toString().trim();
		String phone = mEdtPhone.getText().toString().trim();
		if (email.length() == 0) {
			Statics.showToast(this, "Email is required");
			return;
		}

		if (!Statics.isEmailValid(email)) {
			Statics.showToast(this, "Email is invalid");
			return;
		}
		
		if (firstName.length() == 0) {
			Statics.showToast(this, "First name is required");
			return;
		}
		
		ParseUser user;
		if (ParseUser.getCurrentUser().getObjectId() != null) {
			user = ParseUser.getCurrentUser();
		} else {
			user = new ParseUser();
		}

		
		user.setEmail(email);
		user.put("first_name", firstName);
		user.put("last_name", lastName);
		user.put("phone", phone);
		
		pd.show();
		user.saveInBackground(new SaveCallback() {

			@Override
			public void done(ParseException e) {
				pd.dismiss();
				if (e == null) {
					Statics.showToast(getBaseContext(), "Successful");

					finish();
				} else {
					Statics.showToast(getBaseContext(), "Update fail: " + e.getMessage());
				}
			}
		});
	}
}
